//
//  LFInvestController.m
//  ServiceBaZhou
//
//  Created by 刘江涛 on 2017/8/24.
//  Copyright © 2017年 weierda. All rights reserved.
//

#import "LFInvestController.h"

#import "InvestModel.h"

@interface LFInvestController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong) NSMutableArray *dataArray;

@property (nonatomic,strong) NSArray *typeArray;

@end

@implementation LFInvestController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _typeArray = @[@"公司名称",@"注册号",@"登记机关",@"法定代表人",@"成立日期",@"登记状态",@"注册资本",@"类型",@"公司地址"];
    
    [self createNavigation:@"对外投资"];
    
    [self createTableView];
    
    [self loadData];
    
}

- (void)createTableView{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, Width, Height - 64) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = NO;
    
    [self.view addSubview:_tableView];
}

- (void)loadData{
    NSMutableDictionary *para = [NSMutableDictionary dictionaryWithCapacity:0];
    [para setObject:Token forKey:@"key"];
    [para setObject:_company forKey:@"keyword"];
    [para setObject:@"50" forKey:@"pageSize"];
    
    [CFHttpTools getRequest:@"ECIRelation/SearchInvestment" params:para success:^(id responseObj) {
        //
        CFLog(@"%@",responseObj);
        
        
        if(![responseObj[@"Result"] isKindOfClass:[NSArray class]]){
            if([CFToolUtil isBlankString:responseObj[@"Result"]]){
                UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 64, Width - 100 , Width - 100)];
                imageView.image = [UIImage imageNamed:@"nodata"];
                [self.tableView addSubview:imageView];
                
                UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, Width - 20, Width, 20)];
                label.text = @"暂无数据";
                label.textColor = [UIColor grayColor];
                label.textAlignment = NSTextAlignmentCenter;
                [self.tableView addSubview:label];
            }
        }else{
            
            NSArray *investArray = responseObj[@"Result"];
            
            JSONModelArray *modelArray = [[JSONModelArray alloc]initWithArray:investArray modelClass:[InvestModel class]];
            
            self.dataArray = [NSMutableArray array];
            
            [self.dataArray addObjectsFromArray:(NSArray *)modelArray];
            
            for (NSInteger i = 0; i < _dataArray.count; i ++ ) {
                InvestModel *model = _dataArray[i];
                //@"公司名称",@"注册号",@"登记机关",@"法定代表人",@"成立日期",@"登记状态",@"注册资本",@"类型",@"公司地址"
                model.titleArray = @[model.Name,model.No,model.BelongOrg,model.OperName,[model.StartDate substringToIndex:10],model.Status,model.RegistCapi,model.EconKind,model.Address];
                [_dataArray replaceObjectAtIndex:i withObject:model];
            }
            
            NSLog(@"============%@-----------",_dataArray);
            
            [_tableView reloadData];
            
        }
    } failure:^(NSError *error) {
        //
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _typeArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0){
        return 40;
    }else{
        return 25;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    InvestModel *model = _dataArray[indexPath.section];
    
    if(indexPath.row == 0){
        static NSString *iden = @"iden";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:iden];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.textLabel.text = model.Name;
        cell.textLabel.textColor = [UIColor colorWithRed:0/255.0 green:122/255.0 blue:255/255.0 alpha:1.0];
        cell.textLabel.font = [UIFont systemFontOfSize:14.0];
        return cell;
    }else{
        static NSString *iden2 = @"iden2";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden2];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:iden2];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.textLabel.text = _typeArray[indexPath.row];
        cell.textLabel.font = [UIFont systemFontOfSize:10.0];
        
        cell.detailTextLabel.text = model.titleArray[indexPath.row];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:10];
        
        return cell;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
